package com.haolijun.eecms.controller;

import cn.hutool.json.JSONUtil;
import com.haolijun.eecms.common.R;
import com.haolijun.eecms.controller.form.InsertFacilityForm;
import com.haolijun.eecms.controller.form.SearchFacilityByPageForm;
import com.haolijun.eecms.controller.form.UpdateFacilityForm;
import com.haolijun.eecms.entity.TbFacility;
import com.haolijun.eecms.service.FacilityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

/**
 * @ClassName FacilityController
 * @Description TODO
 * @Author hljstart
 * @Date 2023/3/12 14:46
 * @Version 1.0
 */
@RestController
@RequestMapping("/facility")
@Tag(name = "FacilityController",description = "设备Web接口")
public class FacilityController {

    @Autowired
    private FacilityService facilityService;

    @PostMapping("/searchFacilityByPage")
    @Operation(summary = "查询设备分页")
    public R searchFacilityByPage(@Valid @RequestBody SearchFacilityByPageForm form){
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        R r = facilityService.searchFacilityByPage(param);
        return r;
    }

    @PostMapping("/insert")
    @Operation(summary = "添加设备")
    public R insert(@Valid @RequestBody InsertFacilityForm form){
        TbFacility facility = JSONUtil.parseObj(form).toBean(TbFacility.class);
        int rows = facilityService.insert(facility);
        return rows > 0 ? R.ok() : R.error();
    }

    @GetMapping("/delete/{id}")
    @Operation(summary = "删除设备")
    public R delete(@PathVariable("id") Integer id){
        int rows = facilityService.delete(id);
        return rows > 0 ? R.ok() : R.error();
    }

    @PostMapping("/update")
    @Operation(summary = "更新设备")
    public R update(@Valid @RequestBody UpdateFacilityForm form){
        TbFacility facility = JSONUtil.parseObj(form).toBean(TbFacility.class);
        int rows = facilityService.update(facility);
        return rows > 0 ? R.ok() : R.error();
    }

    @GetMapping("/searchFacilityInfo/{id}")
    @Operation(summary = "查看设备详情")
    public R searchFacilityInfo(@PathVariable("id") Integer id){
        HashMap map = facilityService.searchFacilityInfo(id);
        return R.ok().data(map);
    }
}
